<?php
namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use Request;
use DB;
use GuzzleHttp\Client as GuzzleClient;
use App\Models\ClientConfig;

class CommonController extends Controller {
    //接收手动充值的数据并发送给客户端
    public function addCash(){
        $receive = Request::all();
        $protocol = $receive['protocol'];
        $sign = $receive['sign'];
        $client_url = $receive['client_url'];
        $cash = $receive['cash'];
        $is_update = Db::update('update clients set cash=cash + ' . floatval($cash) . ' where sign = ?', [$sign]);
        if($is_update){
            $order_sn = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);//订单号
            //写入充值记录表
            $pay_log = [
                'pay_type' => 43,
                'total_fee' => $cash,
                'paid_at' => time(),
                'client_url' => $client_url,
                'status' => 1,
                'post_price' => $cash,
                'out_trade_no' => $order_sn,
                'result_code' => 'success',
                'order_at' => time()
            ];
            Db::table('pay_logs')->insert($pay_log);
            //充值记录写入资方表
            $send_url = $protocol.'://'.$client_url.'/api/charge/addcash';
            $params = [
                'form_params' => [
                    'pay_type' => 43,
                    'total_fee' => $cash,
                    'status' => 1,
                    'out_trade_no' => $order_sn,
                ]
            ];
            $this->guzzle($send_url, $params);
        }
        return Response()->json(['info' => '充值成功']);
    }
    //guzzle发送数据
    public function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('POST', $url, $params);
    }
    //get请求
    public function guzzle_get($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('GET', $url, $params);
    }
    //给每一位资方一个扣费标准
    public function deduct(){
        if(request()->method() == 'POST'){
            $receive = Request::all();
            $sign = $receive['sign'];
            $ins_data = [
                'verify'=> $receive['verify'],
                'bindcard'=> $receive['bindcard'],
                'sort'=> $receive['sort'],
                'moxie'=> $receive['moxie'],
                'taobaoweb'=> $receive['taobaoweb'],
                'message'=> $receive['message'],
                'rada'=> $receive['rada'],
                'sign'=> $receive['sign'],
                'bsign' => $receive['bsign'],
                'bairong' => $receive['bairong'],
                'fraud' => $receive['fraud'],
                'carrier' => $receive['carrier'],
                'duotou' => $receive['duotou'],
            ];
            $hasSign = DB::table('deducts')->where('sign', $sign)->first();
            if(count($hasSign)){
                $ins_data['updated_at'] = date('Y-m-d H:i:s');
                $is_ok = DB::table('deducts')->where('sign', $sign)->update($ins_data);
                if($is_ok) return Response()->json(['info' => '成功！']);
                return Response()->json(['info' => '失败！']);
            }else {
                $ins_data['created_at'] = date('Y-m-d H:i:s');
                $ins_data['updated_at'] = date('Y-m-d H:i:s');
                $is_ok = DB::table('deducts')->insert($ins_data);
                if($is_ok) return Response()->json(['info' => '成功！']);
                return Response()->json(['info' => '失败！']);
            }
        }else {
            $sign = Request::get('sign');
            $hasData = DB::table('deducts')->where('sign', $sign)->first();
            if(count($hasData)){
                $hasData->code = 1;
                return Response()->json($hasData);
            }else {
                return Response()->json(['code' => 0]);
            }
        }

    }
    //显示祥情页面
    public function detailShow(){
        $method = request()->method();
        if($method == "GET"){
            $sign = Request::get('sign');
            return view('admin.zhidetail', compact('sign'));
        }elseif($method == 'POST'){
            $sign = Request::post('sign');
            $client = DB::table('clients')->where('sign', $sign)->first();
            $send_url = $client->protocol.'://'.$client->client_url.'/api/other/detail/show';
            $params = [
                'query' => [
                    'sign' => $sign
                ]
            ];
            $detail = $this->guzzle_get($send_url, $params);
            return $detail->getBody()->getContents();
        }
    }
    //设置资方的通讯了的显示和隐藏
    public function close_and_open_contacts(){
        $method = request()->method();
        if($method == "GET"){
            $sign = Request::get('sign');
            $client_config = ClientConfig::where('config', 'contacts')->first();
            $contact = $client_config->value;
            return view('admin.clientConfig', compact('sign', 'contact'));
        }elseif($method == 'POST'){
            $sign = Request::post('sign');
            $type = Request::post('buttype');
            $client = DB::table('clients')->where('sign', $sign)->first();
            $send_url = $client->protocol.'://'.$client->client_url.'/api/common/open/close/tel';
            $params = [
                'query' => [
                    'sign' => $sign,
                    'buttype' => $type
                ]
            ];
            $detail = $this->guzzle($send_url, $params);
            $response = json_decode($detail->getBody()->getContents(), true);
            if($response['code'] == 0){
                if($type == 'open'){
                    ClientConfig::where('config', 'contacts')->update(['value' => 2]);
                }elseif($type == 'close'){
                    ClientConfig::where('config', 'contacts')->update(['value' => 1]);
                }
                return ['code' => 0, 'text' => $response['text']];
            }
            return ['code' => -1, 'text' => '操作失败'];
        }
    }
}